<?php

namespace TuxDrink\TuxDb\Mysql\Drink;

use TuxDrink\TuxDb\TuxDrinkGenericInterface;
use TuxDrink\TuxDb\Mysql\Drink\Model\DrinkGenericTable;
use TuxDrink\TuxDb\Mysql\TuxDbMysqlAbstract;
use Zend\Db\Sql\Where;

class TuxDbMysqlDrinkGeneric extends TuxDbMysqlAbstract implements TuxDrinkGenericInterface {

	/**
	 *
	 * @var TuxDbMysqlImage
	 */
	private $tuxDbMysqlImage;
	
	private $config;
	
	/**
	 * 
	 * @var DrinkGenericTable
	 */
	private $drinkGenericTable;
	
	/**
	 * 
	 * @return \TuxDrink\TuxDb\Mysql\Drink\Model\DrinkGenericTable
	 */
	private function getDrinkGenericTable() {
		if (!$this->drinkGenericTable) {
			$this->drinkGenericTable = new DrinkGenericTable($this->tuxDb->getInstance());
		}
		return $this->drinkGenericTable;
	}
	
	/**
	 * 
	 * @param unknown $config
	 * @param TuxDbMysqlImage $tuxDbMysqlImage
	 */
	public function __construct($config,$tuxDbMysqlImage) {
		parent::__construct($config);
		$this->config=$config;
		$this->tuxDbMysqlImage=$tuxDbMysqlImage;
	}
	
	public function getAllGenericDrinks() {
		$drinkGenericTable = $this->getDrinkGenericTable();
		$select = $drinkGenericTable->newSelect();
		$drinkGenericTable->getSelect($select,array('drink_generic_id'))
		->join('drink_type', 'drink_type.drink_type_id = drink_generic.drink_type_id',array('drink_type_name'))
		->join('drink_flavor_type','drink_flavor_type.drink_flavor_type_id = drink_generic.drink_flavor_type_id',array('drink_flavor_type_name'));
		$resultSet =$drinkGenericTable->selectWith($select);
		if ($resultSet->count() > 0) {
			$rows=$resultSet->toArray();
			return $rows;
		}
		return null;
	}

	public function getAllGenericDrinksJson() {
		$genericDrinks = $this->getAllGenericDrinks();
		if (is_null($genericDrinks)) {
			$genericDrinks=array();
		}
		return json_encode($genericDrinks);
	}	
		
	public function addDrinkGeneric($drinkTypeId,$drinkFlavorTypeId) {
		$this->getDrinkGenericTable()->addDrinkGeneric($drinkTypeId, $drinkFlavorTypeId);
		$lastInsertId=$this->getDrinkGenericTable()->getAdapter()->getDriver()->getLastGeneratedValue();
		return $lastInsertId;
	}
	
	public function getDrinkGenericId($drinkTypeName,$drinkFlavorTypeName) {
		$drinkGenericTable = $this->getDrinkGenericTable();
		$select = $drinkGenericTable->newSelect();
		$drinkGenericTable->getSelect($select,array('drink_generic_id'))
		->join("drink_type", "drink_type.drink_type_id = drink_generic.drink_type_id",array())
		->join('drink_flavor_type', "drink_flavor_type.drink_flavor_type_id=drink_generic.drink_flavor_type_id",array())
		->where(array('drink_type.drink_type_name'=>$drinkTypeName,"drink_flavor_type.drink_flavor_type_name"=>$drinkFlavorTypeName));
		$resultSet = $drinkGenericTable->selectWith($select);
		if ($resultSet->count() > 0) {
			$rows = $resultSet->toArray();
			return $rows[0]['drink_generic_id'];
		} else {
			return null;
		}
	}
	
	public function deleteGenericDrinkById($id) {
		$drinkGenericTable = $this->getDrinkGenericTable();
		$drinkGenericTable->deleteDrinkGeneric($id);
	}
	
	public function getFullDrinkName($id) {
		$drinkGenericTable = $this->getDrinkGenericTable();
		$select = $drinkGenericTable->newSelect();
		$drinkGenericTable->getSelect($select,array())
		->join('drink_type', 'drink_type.drink_type_id = drink_generic.drink_type_id',array('drink_type_name'))
		->join('drink_flavor_type','drink_flavor_type.drink_flavor_type_id = drink_generic.drink_flavor_type_id',array('drink_flavor_type_name'))
		->where(array('drink_generic.drink_generic_id'=>$id));
		$resultSet = $drinkGenericTable->selectWith($select);
		$arr = $resultSet->toArray();
		if (sizeof($arr) != 1) {
			return null;
		}
		else {
			return $arr[0]['drink_type_name'].' - '.$arr[0]['drink_flavor_type_name'];
		}
	}
	
}
